import {getDictList} from "@/api/dict";
import {ref} from "vue";
import type {Dict} from "@/api/dict/types";

/**
 * 字典组合函数
 * @param types
 * @constructor
 */
export const UseDict = (...types: string[]) => {
    const dict = ref<any>({})

    const fetchDict = async (type: string) => {
        dict.value[type] = await getDictList(type)
    }
    types.forEach((type: string) => fetchDict(type))

    const showDictName = (type: string, value: string) => {
        return dict.value[type]?.find((item: Dict) => item.value == value).name
    }

    return {dict, showDictName}
}